package com.example.meterialproject.activitys.transition.demo5

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import androidx.navigation.NavOptions
import androidx.navigation.Navigation
import androidx.navigation.fragment.FragmentNavigatorExtras
import com.example.meterialproject.R
import com.example.meterialproject.exts.addRandomColorView
import com.example.meterialproject.exts.click
import java.util.concurrent.TimeUnit

/**
 *
 * @ClassName: transitionDemo5FormFragment
 * @Author: android 超级兵
 * @CreateDate: 4/28/22$ 10:25 AM$
 * TODO
 */
class TransitionDemo5FromFragment : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View? {
        postponeEnterTransition(0,TimeUnit.MILLISECONDS)
        return inflater.inflate(R.layout.transition_demo_form_5, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val navOptions = NavOptions.Builder()
            .setLaunchSingleTop(true)
            .setEnterAnim(R.anim.enter_in)
            .setPopEnterAnim(R.anim.enter_out)
            .setExitAnim(R.anim.exit_in)
            .setPopExitAnim(R.anim.exit_out)
            .build()

        // 跳转页面
        view.findViewById<View>(R.id.btn).click {
            Navigation.findNavController(it)
                .navigate(R.id.transitionDemo5ToFragment, null, navOptions)
        }

        view.findViewById<ViewGroup>(R.id.linearParent)
            .addRandomColorView { itemView, color, position ->

                val extras = FragmentNavigatorExtras(itemView to "$position")

                Navigation.findNavController(itemView)
                    .navigate(
                        R.id.transitionDemo5ToFragment,
                        bundleOf(
                            TransitionDemo5ToFragment.TRANSITION_NAME to itemView.transitionName,
                            TransitionDemo5ToFragment.BACKGROUND_COLOR to color,
                        ),
                        null,
                        extras
                    )
            }

    }
}